
function loaddata(){
	var emp_id = $("#emp_id").val();
	var allFlag = $("#allFlag").val();
	var pno = $("#pagenum").val();
	var params = {emp_id:emp_id,allFlag:allFlag,pno:pno};
	var url = pms_url + "sys/performance/getPermissions"; 
	var obj = load(url,params);
	var Permissions = obj.Permissions;
	$("#performanceMaintainMonlist").find("tbody").html("");
	$.each(Permissions, function(index, p) {
		var tr ="<tr role='row'>"+
		"<td align='center'>"+p.user_id+"</td>"+
		"<td align='center'>"+p.user_name+"</td>"+
		"<td align='center'>"+p.ope_fk+"</td>"+
		"<td><button type='button' class='btn red btn-outline btn-xs del'>删除</button></td>"+
		"</tr>";
		$("#performanceMaintainMonlist").find("tbody").append(tr);
	});
}



function getUser_name(){

	var emp_id = $("#emp_id1").val();
	if(emp_id.length < 8)return;
	var param = {emp_id:emp_id};
	var url = pms_url + "sys/performance/isUserExist"; 
	$.ajax({  
        type : "post",  
        url : url,  
        dataType: "json", 
        data : param, 
        async : false,  
        success : function(data){ 
	    		if(data.err_code=="1"){
	    			document.getElementById("emp_name1").value = data.err_msg;
	    		}else {
	    			alert("该用户不存在，请检查工号！");
	    			document.getElementById("emp_id1").value = "";
	    		}
        	}
        });				   
	
}




$(function () {

		loaddata();
		
		
		var fab= $("#fab").val(); ;
		if(fab == "ARY"){
			$('#scope1').empty();
			$('#scope1').append("<option value='ARY'>ARY</option>")
		}
		
		
		
		$("#btnReset").on('click', function () {
			$("#searchform input").val("");
			$("#searchform select").val("");
		});	

		$('#btnAdd').on('click', function () {
			$('#permissionsAdd').find("input").val("");
			$('#permissionsAdd').find("textarea").val("");
			$('#permissionsAdd').modal("show");
		});

		$('#btnSearch').on('click', function () {
			$("#pagenum").val(1);
			loaddata();
			
		});
		$("#btnAddData").on('click', function () {
			var bootstrapValidator = $("#basicform").data('bootstrapValidator');
			   bootstrapValidator.validate();
			   if(bootstrapValidator.isValid()){
					var scope=$("#scope1").val();
					var ope_id = $("#ope_id_add").val();
					var emp_id = $("#emp_id1").val();
					var emp_name = $("#emp_name1").val();
					
					var param = {scope:scope,ope_id:ope_id,emp_id:emp_id,emp_name:emp_name};
					var url = pms_url + "sys/performance/addPermissions"; 
					$.ajax({  
				        type : "post",  
				        url : url,  
				        dataType: "json", 
				        data : param, 
				        async : false,  
				        success : function(data){ 
					    		if(data.err_msg=="success"){
					    			alert("保存成功");
									$('#modalAdd').modal("hide");
									window.location.reload();
					    		}else {
					    			alert("您没有权限创建！");
					    		}
				        	}
				        });				   
			   }
			   else return;
		});
		
		 $("#emp_id1").keyup(function () {
			var emp_id = $("#emp_id1").val();
			var users = $("#users"); 
			users.empty();
			
			var params = {emp_id:emp_id};
			var url = pms_url + "sys/performance/getUser_ID"; 
			var obj = load(url,params);
			var User_ID = obj.User_ID;
			$.each(User_ID, function(index, u) {
				users.append("<option>"+u.user_id+"</option>");
			});
		});
		$(document).on('click', ".del",function () {
			if(!confirm("是否确认删除！")){
				return;
			}
			var emp_id= $(this).parent().parent().children("td:eq(0)").html();
			var param = {emp_id:emp_id};
			var delurl = pms_url + "sys/performance/deletePermissions";
			$.post(delurl, param, function(data) {
				if(data.err_msg=="success"){
					alert("删除成功！");
					loaddata();
				}
			},"json");
		});
		
		
		
	
		$('#basicform').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok validator',
                invalid: 'glyphicon glyphicon-remove validator',
                validating: 'glyphicon glyphicon-refresh validator'
            },
            fields: {
            	scope1: {
                    message: 'The scope is not valid',
                    validators: {
                        notEmpty: {
                            message: '请选择工厂'
                        }
                    }
            	},
            	
                emp_id1: {
                    message: 'The emp_id is not valid',
                    validators: {
                        notEmpty: {
                            message: '请填写工号'
                        }
                    }
                },
                
                ope_id_add: {
                    message: 'The ope_id is not valid',
                    validators: {
                        notEmpty: {
                            message: '请选择站点'
                        }
                    }
                },
                emp_name1: {
                    message: 'The emp_name is not valid',
                    validators: {
                        notEmpty: {
                            message: '请填写姓名'
                        }
                    }
                }
             
                
            }
		});
		
		
		
		
		
		
		
		/*$("#emp_id1").oninput(function(){
			var emp_id = $("#emp_id1").val();
			
			var param = {emp_id:emp_id};
			var url = pms_url + "lcm/performance/isUserExist"; 
			$.ajax({  
		        type : "post",  
		        url : url,  
		        dataType: "json", 
		        data : param, 
		        async : false,  
		        success : function(data){ 
			    		if(data.err_code=="1"){
			    			document.getElementById("emp_name1").value = data.err_msg;
			    		}else {
			    			alert("该用户不存在，请检查工号！");
			    			document.getElementById("emp_id1").value = "";
			    		}
		        	}
		        });				   
			});*/
		
		
		
	});